package cn.hs.ph.servlet;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//关联门店接口实现
@WebServlet("/associatedShop")
public class AssociatedShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CommunityDao communityDao = new CommunityDaoImpl();
        if ("selectShop".equals(action)) {
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            String communityId = req.getParameter("communityId");
            //封装到map集合中
            Map<String, String> map = new HashMap<>();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("shopName", shopName);
            map.put("communityId", communityId);
            int id = Integer.parseInt(communityId);
            //连接数据库
            List<Shop> list = communityDao.selectShop(map);
            int total = new CommunityDaoImpl().countShop(id);
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("delete".equals(action)) {//删除关联门店
            String communityId = req.getParameter("panghuCommunityId");
            String ShopId = req.getParameter("panghuShopId");
            Map<String, String> map = new HashMap<>();
            map.put("communityId", communityId);
            map.put("ShopId", ShopId);
            int num = new CommunityDaoImpl().deleteShop(map);
            Result result = null;
            if (num != 0) {
                result = Result.success("删除成功");
            } else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("addShop".equals(action)) {//添加尚未关联的门店
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            String communityId = req.getParameter("communityId");
            Map<String, String> map = new HashMap<>();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("shopName", shopName);
            map.put("communityId", communityId);
            List<Shop> list = new CommunityDaoImpl().findNotAssociatedShop(map);
            int total = new CommunityDaoImpl().selectCountShop(map);
            //给出响应
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("addAssociatedShop".equals(action)) {//保存添加关联门店
            int num = 0;
            int com = 0;
            String sid = req.getParameter("fendeShopCommunityList");
            String[] sids = sid.split(",");
            int communityId = Integer.parseInt(req.getParameter("panghuCommunityId"));
            for (String shopId : sids) {
                num += new CommunityDaoImpl().addAssociatedShop(Integer.parseInt(shopId), communityId);
                System.out.println(num+"-"+shopId);
                com+=new CommunityDaoImpl().addAssociatedCommunity(communityId);
                System.out.println(com);
            }
            Result result=null;
            if (num!=0&&com!=0){
                 result = Result.success("添加成功");
            }else {
                result = Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
